<?php
namespace app\api\model\wine;

use app\common\model\BaseModel;

class Dermember extends BaseModel{
    protected $autoWriteTimestamp = true;
    public function user(){
        return $this->hasOne(User::class,'user_id','user_id')
            ->bind(['mobile','nick_name']);
    }
    //添加
    public function add($param){
        $data = [
            'store_id' => getStoreId(),
            'pid'=> $param['pid'],
            'user_id'=> $param['user_id'],
            'idCard'=> $param['idCard'] ?? ''
        ];
        try{
            if(isset($param['id'])){
                return $this->where(['id'=>$param['id']])->save($data);
            }else{
                //创建经销商，如果身份证相同则更新数据
                $r = $this->where(['user_id'=>$param['user_id']])->findOrEmpty();
                if($r->isEmpty()){
                    return $this->save($data);
                }
                return false;
            }
        }catch (\Exception $exception){
            throw new \think\Exception('添加或更新数据异常', 10006);
        }

    }
    //获取经销商信息
    public function getDerInfo($user_id){
        return $this->where(['user_id'=>$user_id,'store_id'=>self::$storeId])
            ->with('user')
            ->find();
    }
}
